/**
 *  _fopen()
 *  viz file_open() {source/core/filesys/files.h}
 */
FILE far *_fopen(char far *filename, char far *mode) {
  return file_open(filename, mode);
}

/**
 *  _fread()
 *  viz file_read() {source/core/filesys/files.h}
 */
void far *_fread(char far *buffer, int size, int n, FILE far *stream) {
  return file_read(buffer, size*n, stream);
}

/**
 *  _fgetc()
 *  sejme jeden znak ze streamu
 */
char _fgetc(FILE far *stream) {
  char small_stream = (char)stream;
  char result;

  if(small_stream == stdin) {
    result = getchar();
  } else {
    file_read(&result, 1, stream);
  }

  return result;
}

/**
 *  _fgets()
 *  sejme sled znaku ze streamu, omezenych poctem <count>
 *  nebo kdyz narazi na \n
 */
void far *_fgets(char far *buffer, int count, FILE far *stream) {
  char small_stream = (char)stream;
  unsigned int  i;

  if(small_stream == stdin) {
    _gets(buffer, count);
  } else {
     for(i=0; i<count; i++) {
       buffer[i] = _fgetc(stream);
       if(buffer[i] == '\r' || buffer[i] == '\n')
         break;
     }
   }

  return buffer;
}

/**
 *  _fwrite()
 *  viz file_write() {source/core/filesys/files.h}
 *  pokud je <stream> roven [stderr] nebo [stdout],
 *  vypise se <buffer> do znaku <size> na monitor
 */
void _fwrite(char far *buffer, int size, int n, FILE far *stream) {
  char small_stream = (char)stream;
  unsigned int  i;

  if(small_stream == stdout || small_stream == stderr) {
     for(i=0; i<=size; i++)
       putchar(buffer[i]);
   } else {
     file_write(buffer, size*n, stream);
   }

  return;
}

/**
 *  _fseek()
 *  v souboru <stream> najede ukazatelem na <seek>
 *  pokud je overflow = true, soubor muze "zajet" i za aktualni konec souboru
 *  IN:   <seek>    - absolutni pozice v souboru, na kterou se nastavi ukazatel
 *        <stream>  - ukazatel na strukturu souboru
 *        <whence> -  odkud se bude seekovat
 *  OUT:  [int] navratove hodnoty:  0 - v poradku
 *                                  1 - chyba (konec souboru, chybny klastr...)
 */
int _fseek(FILE far *stream, int seek, int whence) {
  return file_seek(stream, seek, whence);
}

/**
 *  _fputc()
 *  zapise jeden znak do streamu
 */
void _fputc(char ch, FILE far *stream) {
  char small_stream = (char)stream;

  if(small_stream == stdout || small_stream == stderr) {
    putchar(ch);
  } else {
    file_write(&ch, 1, stream);
  }

  return;
}

/**
 *  _fputs()
 *  zapise retezec ukonceny \0 do streamu
 */
void _fputs(char far *buffer, FILE far *stream) {
  char small_stream = (char)stream;

  if(small_stream == stdout || small_stream == stderr) {
    puts(buffer);
  } else {
    _fwrite(buffer, sizeof(char), strlen(buffer), stream);
  }

  return;
}

/**
 *  _fclose()
 *  viz file_close() {source/core/filesys/files.h}
 */
void _fclose(FILE far *stream) {
  file_close(stream);
  return;
}
